<?

//Data loading
$title='STATEMENT OF CASH FLOWS (INDIRECT)';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new GridPDFReport("P","mm","Letter");
$fromDate = GetParam("fromDate","");
$toDate  = GetParam("toDate","");
$pdf->AddPage();
//$Result = GridList ();
$pdf->GridTable();
$pdf->Output();


function Neraca ($PYear,$PMonth) {
    global $DBConnection;

    //$TEMP = "TEMPORARY";

    $Year   = $PYear;
    $LastYear = $Year-1;
    $Month  = $PMonth;
    if ($Month=="AUDIT") $Month=13;
    if ($Month<10) $Month = "0".$Month;

    if (($Month-1)<10) $LastMonth = "0".($Month-1);
    else $LastMonth = $Month-1;

    if ($Month<2) $LastMonth = "awal";
    else {$LastMonth = "aku".$LastMonth;}

    $Mutasi = "(mutd".$Month."-"."mutk".$Month.") as mutasi";
    $CurrentMonth = "aku".$Month." as cmonth";

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp_last`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
        `lastmonth` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp_last` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp_last
        SELECT skada, $CurrentMonth, ktrasl
        FROM tblcoa WHERE tipe=1 AND ledger='$LastYear'
    ";

    if (GetParam("office",""))  $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp
        SELECT tblcoa.skada, tblcoa.pathprn, tblcoa.namacc, tblcoa.parent, tblcoa.printing, tblcoa.jumlah, tblcoa.grup, $CurrentMonth, tblcoatmp_last.akumulasi, $LastMonth, tblcoa.ktrasl
        FROM tblcoa LEFT JOIN tblcoatmp_last ON tblcoatmp_last.skada=tblcoa.skada AND tblcoatmp_last.ktrasl=tblcoa.ktrasl
        WHERE tipe=1 AND ledger='$Year'
    ";

    if (GetParam("office",""))      $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoa_totaltmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoa_totaltmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `mutd` decimal( 16,4) NOT NULL,
        `mutk` decimal( 16,4) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
        `lastmonth` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL  = "
        INSERT INTO tblcoa_totaltmp
        SELECT tblcoatmp.skada, tblcoatmp.pathprn, tblcoatmp.namacc, tblcoatmp.parent, tblcoatmp.printing,
                tblcoatmp.jumlah, tblcoatmp.grup, sum(mutd$Month), sum(mutk$Month), sum(aku$Month), lastakumulasi, sum($LastMonth), tblcoa.ktrasl
        FROM tblcoatmp, tblcoa
        WHERE tblcoa.pathprn like concat(tblcoatmp.pathprn,'%') AND tblcoatmp.ktrasl=tblcoa.ktrasl
    ";

    if (GetParam("office","")) $SQL .= " AND tblcoa.ktrasl='".GetParam("office","")."' ";

    $SQL .= "GROUP BY tblcoatmp.skada";
    //print $SQL."<p>\n";die();
    $DBConnection->dbc->query($SQL);
}

function GridList () {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    list($day,$month,$year) = explode("/",GetParam("fromDate",""));
    $fromDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
    list($day,$month,$year) = explode("/",GetParam("toDate",""));
    $toDate   = date('Y-m-d',mktime(0,0,0,$month,$day,$year));

    $wherenobkt = "(nobkt like 'ZTB%')";
    $SQL = "SELECT *, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate FROM ais_memorial_z WHERE ".$wherenobkt." AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND urut<>999";
    if (GetParam("office",""))
    $SQL .= " AND ktrasl= '".GetParam("office","")."'";

    if (GetParam("skada",""))
    $SQL .= " AND skada= '".GetParam("skada","")."'";

    $SQL .= " ORDER BY nobkt";
    //print $SQL."<br><br>\n\n\n\n";die();
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    return $MyResult;
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return strtoupper(date("d F Y", mktime(0, 0, 0, $month+1, 0, $year)));

}

function BuildMonthYear ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return strtoupper(date("F Y", mktime(0, 0, 0, $month+1, 0, $year)));
}


    function GetNetIncome($Year,$Month) {
      global $DBConnection;
      Neraca($Year,$Month);
      $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
      $pendapatan = $DBConnection->dbc->get_var($SQL);
      $pendapatan = $pendapatan<0?$pendapatan*-1:$pendapatan;

      $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
      $biaya = $DBConnection->dbc->get_var($SQL);

      $gross_profit=$pendapatan-$biaya;

      $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80'";
      $biaya_umum = $DBConnection->dbc->get_var($SQL);

      $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='90'";
      $biaya_other = $DBConnection->dbc->get_var($SQL);

      $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='91'";
      $biaya_pajak = $DBConnection->dbc->get_var($SQL);

      if ($Month>1) {
          Neraca($Year,$Month-1);
          $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
          $last_pendapatan = $DBConnection->dbc->get_var($SQL);
          $last_pendapatan = $last_pendapatan<0?$last_pendapatan*-1:$last_pendapatan;

          $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
          $last_biaya = $DBConnection->dbc->get_var($SQL);

          $last_gross_profit=$last_pendapatan-$last_biaya;


          $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80'";
          $last_biaya_umum = $DBConnection->dbc->get_var($SQL);

          $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='90'";
          $last_biaya_other = $DBConnection->dbc->get_var($SQL);

          $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='91'";
          $last_biaya_pajak = $DBConnection->dbc->get_var($SQL);
      }

      return $NetIncome=($gross_profit-$biaya_umum-$biaya_other-$biaya_pajak)-($last_gross_profit-$last_biaya_umum-$last_biaya_other-$last_biaya_pajak);
    }

    function GetDepreciation($Year,$Month) {
        global $DBConnection;
        Neraca($Year,$Month);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='1002' OR skada='1004'";
        $current_penyusutan_activa_tetap = $DBConnection->dbc->get_var($SQL);
        $current_penyusutan_activa_tetap = $current_penyusutan_activa_tetap==""?0:$current_penyusutan_activa_tetap;

        if ($Month>1) {
            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='1002' OR skada='1004'";
        }else {
            $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='1002' OR skada='1004'";
        }
        $last_penyusutan_activa_tetap = $DBConnection->dbc->get_var($SQL);
        $last_penyusutan_activa_tetap = $last_penyusutan_activa_tetap==""?0:$last_penyusutan_activa_tetap;


        Neraca($Year,$Month);
        $Depreciation = $current_penyusutan_activa_tetap-$last_penyusutan_activa_tetap;
        return $Depreciation*-1;
    }

    function GetIncreaseDecreaseAR($Year,$Month) {
        global $DBConnection;
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0006'";
        $current_AR = $DBConnection->dbc->get_var($SQL);
        $current_AR = $current_AR==""?0:$current_AR;
        if ($Month>1) {
            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0006'";
        } else {
            $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='0006'";
        }
        $last_AR = $DBConnection->dbc->get_var($SQL);
        $last_AR = $last_AR==""?0:$last_AR;

        Neraca($Year,$Month);
        $IncreaseDecreaseAR = $current_AR-$last_AR;

        return $IncreaseDecreaseAR*-1;
    }

    function GetIncreaseDecreaseOtherCurAsset($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0007' OR skada='0008' OR skada='0009' OR skada='0010' OR skada='0011' OR skada='0012' OR skada='0004'";
        $other_current_asset = $DBConnection->dbc->get_var($SQL);
        $other_current_asset = $other_current_asset==""?0:$other_current_asset;
        if ($Month>1) {

            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0007' OR skada='0008' OR skada='0009' OR skada='0010' OR skada='0011' OR skada='0012' OR skada='0004'";
        } else {
            $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='0007' OR skada='0008' OR skada='0009' OR skada='0010' OR skada='0011' OR skada='0012' OR skada='0004'";
            }
        $other_last_asset = $DBConnection->dbc->get_var($SQL);
        $other_last_asset = $other_last_asset==""?0:$other_last_asset;

        Neraca($Year,$Month);
        $OtherCurAsset = $other_current_asset-$other_last_asset;

        return $OtherCurAsset*-1;
    }

    function GetAmortization($Year,$Month) {
        global $DBConnection;
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='710110'";
        $current_amortization = $DBConnection->dbc->get_var($SQL);
        $current_amortization = $current_amortization==""?0:$current_amortization;

        if ($Month>1) {
            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='710110'";
        } else {
                $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='710110'";
        }
            $last_amortization = $DBConnection->dbc->get_var($SQL);
            $last_amortization = $last_amortization==""?0:$last_amortization;

        Neraca($Year,$Month);
        $Amortization = $current_amortization-$last_amortization;

        return $Amortization;
    }

    function GetIncreaseDecreaseOtherAsset($Year,$Month) {
        global $DBConnection;
        $Amortization = GetAmortization($Year,$Month);

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='2001' OR skada='2002' OR skada='2004' OR skada='2005'";
        $other_current_asset = $DBConnection->dbc->get_var($SQL);
        $other_current_asset = $other_current_asset==""?0:$other_current_asset;
        if ($Month>1) {
            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='2001' OR skada='2002' OR skada='2004' OR skada='2005'";
        } else {
            $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='2001' OR skada='2002' OR skada='2004' OR skada='2005'";
        }
        $other_last_asset = $DBConnection->dbc->get_var($SQL);
        $other_last_asset = $other_last_asset==""?0:$other_last_asset;

        Neraca($Year,$Month);
        if ($Amortization>0)
        $OtherCurAsset = $other_current_asset-$other_last_asset+$Amortization;
        else
        $OtherCurAsset = $other_current_asset-$other_last_asset-$Amortization;

        return $OtherCurAsset*-1;
    }

    function GetIncreaseDecreaseAP($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='3002'";
        $current_ap = $DBConnection->dbc->get_var($SQL);
        $current_ap = $current_ap==""?0:$current_ap;
        if ($Month>1) {
            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='3002'";
        } else {
            $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='3002'";
            }
        $last_ap = $DBConnection->dbc->get_var($SQL);
        $last_ap = $last_ap==""?0:$last_ap;

        Neraca($Year,$Month);
        $ap = $current_ap-$last_ap;

        return $ap*-1;
    }

    function GetIncreaseDecreaseOtherCurLiabilities($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='3001' OR skada='3003' OR skada='3004' OR skada='3008' OR skada='4101' OR skada='3005'";
        $Current_OtherCurLiabilities = $DBConnection->dbc->get_var($SQL);
        $Current_OtherCurLiabilities = $Current_OtherCurLiabilities==""?0:$Current_OtherCurLiabilities;
        if ($Month>1) {
        Neraca($Year,$Month-1);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='3001' OR skada='3003' OR skada='3004' OR skada='3008' OR skada='4101' OR skada='3005'";
    } else {
        $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='3001' OR skada='3003' OR skada='3004' OR skada='3008' OR skada='4101' OR skada='3005'";
        }
        $Last_OtherCurLiabilities = $DBConnection->dbc->get_var($SQL);
        $Last_OtherCurLiabilities = $Last_OtherCurLiabilities==""?0:$Last_OtherCurLiabilities;

        Neraca($Year,$Month);
        $IncreaseDecreaseOtherCurLiabilities = $Current_OtherCurLiabilities-$Last_OtherCurLiabilities;

        return $IncreaseDecreaseOtherCurLiabilities*-1;
    }

    function GetPurchasedFixedAsset($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(mutd) FROM tblcoa_totaltmp WHERE skada='1001' OR skada='1003'";
        $Current_PurchasedFixedAsset = $DBConnection->dbc->get_var($SQL);
        $Current_PurchasedFixedAsset = $Current_PurchasedFixedAsset==""?0:$Current_PurchasedFixedAsset;
        $Current_PurchasedFixedAsset;
        $PurchasedFixedAsset = $Current_PurchasedFixedAsset;

        return $PurchasedFixedAsset*-1;
    }

    function GetSalesFixedAsset($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(mutk) FROM tblcoa_totaltmp WHERE skada='1001' OR skada='1003'";
        $Current_SalesFixedAsset = $DBConnection->dbc->get_var($SQL);
        $Current_SalesFixedAsset = $Current_SalesFixedAsset==""?0:$Current_SalesFixedAsset;
        $SalesFixedAsset = $Current_SalesFixedAsset;

        return $SalesFixedAsset;
    }

    function GetDefferedExpense($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='2003'";
        $Current_DefferedExpense = $DBConnection->dbc->get_var($SQL);
        $Current_DefferedExpense = $Current_DefferedExpense==""?0:$Current_DefferedExpense;
        if ($Month>1) {
        Neraca($Year,$Month-1);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='2003'";
    } else {
        $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='2003'";
        }
        $Last_DefferedExpense = $DBConnection->dbc->get_var($SQL);
        $Last_DefferedExpense = $Last_DefferedExpense==""?0:$Last_DefferedExpense;

        Neraca($Year,$Month);
        $DefferedExpense = $Current_DefferedExpense-$Last_DefferedExpense;

        return $DefferedExpense*-1;
    }

    function GetPaidInCapital($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5001'";
        $Current_PaidInCapital = $DBConnection->dbc->get_var($SQL);
        $Current_PaidInCapital = $Current_PaidInCapital==""?0:$Current_PaidInCapital;
        if ($Month>1) {
        Neraca($Year,$Month-1);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5001'";
    } else {
        $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='5001'";
        }
        $Last_PaidInCapital = $DBConnection->dbc->get_var($SQL);
        $Last_PaidInCapital = $Last_PaidInCapital==""?0:$Last_PaidInCapital;

        Neraca($Year,$Month);
        $PaidInCapital = $Current_PaidInCapital-$Last_PaidInCapital;

        return $PaidInCapital;
    }

    function GetRevFixedAsset($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5002'";
        $Current_RevFixedAsset = $DBConnection->dbc->get_var($SQL);
        $Current_RevFixedAsset = $Current_RevFixedAsset==""?0:$Current_RevFixedAsset;
        if ($Month>1) {
        Neraca($Year,$Month-1);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5002'";
    } else {
        $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='5002'";
        }
        $Last_RevFixedAsset = $DBConnection->dbc->get_var($SQL);
        $Last_RevFixedAsset = $Last_RevFixedAsset==""?0:$Last_RevFixedAsset;

        Neraca($Year,$Month);
        $RevFixedAsset = $Current_RevFixedAsset-$Last_RevFixedAsset;

        return $RevFixedAsset;
    }

    function GetRelatedPartyTrans($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5003'";
        $Current_RelatedPartyTrans = $DBConnection->dbc->get_var($SQL);
        $Current_RelatedPartyTrans = $Current_RelatedPartyTrans==""?0:$Current_RelatedPartyTrans;

        if ($Month>1) {
        Neraca($Year,$Month-1);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='5003'";
    } else {
        $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='5003'";
        }
        $Last_RelatedPartyTrans = $DBConnection->dbc->get_var($SQL);
        $Last_RelatedPartyTrans = $Last_RelatedPartyTrans==""?0:$Last_RelatedPartyTrans;

        Neraca($Year,$Month);
        $RelatedPartyTrans = $Current_RelatedPartyTrans-$Last_RelatedPartyTrans;

        return $RelatedPartyTrans;
    }

    function GetReceivedLoan($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(mutk) FROM tblcoa_totaltmp WHERE skada='3006' OR skada='3007' OR skada='4001' OR skada='4003'";
        $Current_ReceivedLoan = $DBConnection->dbc->get_var($SQL);
        $Current_ReceivedLoan = $Current_ReceivedLoan==""?0:$Current_ReceivedLoan;

        $ReceivedLoan = $Current_ReceivedLoan;

        return $ReceivedLoan;
    }

    function GetPaymentLoan($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(mutd) FROM tblcoa_totaltmp WHERE skada='3006' OR skada='3007' OR skada='4001' OR skada='4003'";
        $Current_PaymentLoan = $DBConnection->dbc->get_var($SQL);
        $Current_PaymentLoan = $Current_PaymentLoan==""?0:$Current_PaymentLoan;

        $PaymentLoan = $Current_PaymentLoan*-1;

        return $PaymentLoan;
    }

    function GetPaymentDividen($Year,$Month) {
        global $DBConnection;

        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='9006'";
        $Current_PaymentDividen = $DBConnection->dbc->get_var($SQL);
        $Current_PaymentDividen = $Current_PaymentDividen==""?0:$Current_PaymentDividen;
        if ($Month>1) {
        Neraca($Year,$Month-1);
        $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='9006'";
    } else {
        $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE skada='9006'";
        }
        $Last_PaymentDividen = $DBConnection->dbc->get_var($SQL);
        $Last_PaymentDividen = $Last_PaymentDividen==""?0:$Last_PaymentDividen;

        Neraca($Year,$Month);
        $PaymentDividen = $Current_PaymentDividen-$Last_PaymentDividen;

        return $PaymentDividen;
    }

    function GetBeginingOfPeriod($Year,$Month) {
        global $DBConnection;
        Neraca($Year,$Month);
        if ($Month>1) {
            Neraca($Year,$Month-1);
            $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE (skada='0001' OR skada='0002' OR skada='0003' OR skada='0005')";
        }else {

            $SQL = "SELECT SUM(lastmonth) FROM tblcoa_totaltmp WHERE (skada='0001' OR skada='0002' OR skada='0003' OR skada='0005')";
        }
        $Last_BeginingOfPeriod = $DBConnection->dbc->get_var($SQL);

        //print $Last_BeginingOfPeriod;
        $Last_BeginingOfPeriod = $Last_BeginingOfPeriod==""?0:$Last_BeginingOfPeriod;



        return $Last_BeginingOfPeriod;
    }

    function GetNeraca($Year,$Month) {

        $Year = date("Y", mktime(0, 0, 0, $Month, 1, $Year));
        $Month = date("n", mktime(0, 0, 0, $Month, 1, $Year));

        $result["NET_INCOME"] = GetNetIncome($Year,$Month);
        $result["DEPRECIATION"] = GetDepreciation($Year,$Month);
        $result["INC_DEC_AR"] = GetIncreaseDecreaseAR($Year,$Month);
        $result["OTHER_CUR_ASSET"] = GetIncreaseDecreaseOtherCurAsset($Year,$Month);
        $result["OTHER_ASSET"] = GetIncreaseDecreaseOtherAsset($Year,$Month);
        $result["INC_DEC_AP"] = GetIncreaseDecreaseAP($Year,$Month);
        $result["INC_DEC_OCL"] = GetIncreaseDecreaseOtherCurLiabilities($Year,$Month);
        $result["AMORTIZATION"] = GetAmortization($Year,$Month);
        $result["NET_FROM_OA"] = $result["NET_INCOME"]+$result["DEPRECIATION"]+$result["INC_DEC_AR"]+$result["OTHER_CUR_ASSET"]+$result["OTHER_ASSET"]+$result["INC_DEC_AP"]+$result["INC_DEC_OCL"]+$result["AMORTIZATION"];
        $result["PURCHASED_FIXED_ASSET"] = GetPurchasedFixedAsset($Year,$Month);
        $result["SALES_FIXED_ASSET"] = GetSalesFixedAsset($Year,$Month);
        $result["DEFFERED_EXPENSE"] = GetDefferedExpense($Year,$Month);
        $result["NET_FROM_IA"] = $result["PURCHASED_FIXED_ASSET"]+$result["SALES_FIXED_ASSET"]+$result["DEFFERED_EXPENSE"];
        $result["PAID_IN_CAPITAL"] = GetPaidInCapital($Year,$Month);
        $result["REV_FIXED_ASSET"] = GetRevFixedAsset($Year,$Month);
        $result["RELATED_PARTY_TRANS"] = GetRelatedPartyTrans($Year,$Month);
        $result["PAYMENT_DIVIDEN"] = GetPaymentDividen($Year,$Month);
        $result["RECEIVED_LOAN"] = GetReceivedLoan($Year,$Month);
        $result["PAYMENT_LOAN"] = GetPaymentLoan($Year,$Month);
        $result["NET_FROM_FA"] = $result["PAID_IN_CAPITAL"]+$result["REV_FIXED_ASSET"]+$result["REV_FIXED_ASSET"]+$result["RELATED_PARTY_TRANS"]+$result["RECEIVED_LOAN"]+$result["PAYMENT_LOAN"];
        $result["INC_DEC_PERIOD"] = $result["NET_FROM_OA"]+$result["NET_FROM_IA"]+$result["NET_FROM_FA"];
        $result["BEGINING_PERIOD"] = GetBeginingOfPeriod($Year,$Month);
        $result["ENDING_PERIOD"] = $result["INC_DEC_PERIOD"]+$result["BEGINING_PERIOD"];

        return $result;
    }

class GridPDFReport extends PDF {
    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        //$this->SetLeftMargin(5);
        $Month = GetParam("m","");
        $Year  = GetParam("y","");
        $this->SetFont('arialn','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);
        $this->Cell(200,5,"PERIOD: ".BuildPeriod ($Year,$Month),0,1,'C',0);
        $this->SetLeftMargin(5);
        $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(5,30,210,30);
        $this->Ln();
        $this->Ln();
        //Header
        //Column widths
        $w=array(80,30,30,30);
        //$this->SetFont('arialn','',10);

        $this->Cell($w[0],7,"DESCRIPTION",1,0,'C',0);
        $this->Cell($w[1],7,BuildMonthYear ($Year, $Month-2),1,0,'C',0);
        $this->Cell($w[2],7,BuildMonthYear ($Year, $Month-1),1,0,'C',0);
        $this->Cell($w[3],7,BuildMonthYear ($Year, $Month),1,0,'C',0);
        $this->Ln();

        $this->SetFont('arialn','',8);
    }


    function GridTable() {
        //Column widths
        $Year = GetParam("y","");
        $Month = GetParam("m","");

        $Current = GetNeraca($Year,$Month);
        $Last    = GetNeraca($Year,$Month-1);
        $Last2   = GetNeraca($Year,$Month-2);

        $w=array(80,30,30,30);
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'C',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"CASH FLOWS FROM OPERATING ACTIVITIES :","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Cell($w[3],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],5,"   NET INCOME","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["NET_INCOME"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["NET_INCOME"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["NET_INCOME"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   ADD (DEDUCT) ADJUSTMENTS :","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'C',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     DEPRECIATION","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["DEPRECIATION"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["DEPRECIATION"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["DEPRECIATION"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     AMORTIZATION","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["AMORTIZATION"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["AMORTIZATION"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["AMORTIZATION"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     INCREASE (DECREASE) ACCOUNT RECEIVABLES","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["INC_DEC_AR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["INC_DEC_AR"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["INC_DEC_AR"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     INCREASE (DECREASE) OTHER CURRENT ASSETS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["OTHER_CUR_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["OTHER_CUR_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["OTHER_CUR_ASSET"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     INCREASE (DECREASE) OTHER ASSETS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["OTHER_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["OTHER_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["OTHER_ASSET"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     INCREASE (DECREASE) ACCOUNT PAYABLES","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["INC_DEC_AP"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["INC_DEC_AP"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["INC_DEC_AP"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"     INCREASE (DECREASE) OTHER CURRENT LIABILITIES","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["INC_DEC_OCL"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["INC_DEC_OCL"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["INC_DEC_OCL"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'C',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],7,"     NET CASH FLOWS FROM OPERATING ACTIVITIES",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Last2["NET_FROM_OA"]),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Last["NET_FROM_OA"]),1,0,'R',0);
        $this->Cell($w[3],7,FormatNumber($Current["NET_FROM_OA"]),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'C',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"CASH FLOWS FROM INVESTING ACTIVITIES :","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"    PURCHASE OF FIXED ASSETS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["PURCHASED_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["PURCHASED_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["PURCHASED_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"    SALES OF FIXED ASSETS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["SALES_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["SALES_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["SALES_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"    INCREASE DEFFERED EXPENSES","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["DEFFERED_EXPENSE"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["DEFFERED_EXPENSE"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["DEFFERED_EXPENSE"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'C',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],7,"     NET CASH FLOWS FROM INVESTING ACTIVITIES",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Last2["NET_FROM_IA"]),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Last["NET_FROM_IA"]),1,0,'R',0);
        $this->Cell($w[3],7,FormatNumber($Current["NET_FROM_IA"]),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'C',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"CASH FLOWS FROM FINANCING ACTIVITIES :","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   ADDITIONAL PAID IN CAPITAL","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["PAID_IN_CAPITAL"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["PAID_IN_CAPITAL"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["PAID_IN_CAPITAL"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   REVALUATION FIXED ASSETS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["REV_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["REV_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["REV_FIXED_ASSET"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   INCREASE (DECREASE) RELATED PARTY TRANSACTION","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["RELATED_PARTY_TRANS"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["RELATED_PARTY_TRANS"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["RELATED_PARTY_TRANS"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   PAYMENTS OF DIVIDENDS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["PAYMENT_DIVIDEN"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["PAYMENT_DIVIDEN"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["PAYMENT_DIVIDEN"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   RECEIVED FROM LOAN","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["RECEIVED_LOAN"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["RECEIVED_LOAN"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["RECEIVED_LOAN"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   REPAYMENT OF LONG TERM DEBTS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["PAYMENT_LOAN"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["PAYMENT_LOAN"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["PAYMENT_LOAN"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Cell($w[3],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],7,"     NET CASH FLOWS FROM FINANCING ACTIVITIES",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Last2["NET_FROM_FA"]),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Last["NET_FROM_FA"]),1,0,'R',0);
        $this->Cell($w[3],7,FormatNumber($Current["NET_FROM_FA"]),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Cell($w[3],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],5,"CASH AND CASH EQUIVALENTS :","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Cell($w[3],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"    INCREASE (DECREASE) DURING THE PERIOD","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["INC_DEC_PERIOD"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["INC_DEC_PERIOD"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["INC_DEC_PERIOD"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"    BEGINNING OF THE PERIOD","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Last2["BEGINING_PERIOD"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Last["BEGINING_PERIOD"]),"LR",0,'R',0);
        $this->Cell($w[3],5,FormatNumber($Current["BEGINING_PERIOD"]),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Cell($w[3],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],7,"ENDING OF THE PERIOD",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Last2["ENDING_PERIOD"]),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Last["ENDING_PERIOD"]),1,0,'R',0);
        $this->Cell($w[3],7,FormatNumber($Current["ENDING_PERIOD"]),1,0,'R',0);
        $this->Ln();

        /*
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Cell($w[3],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Cell($w[3],5,"","LR",0,'L',0);
        $this->Ln();
*/
        //Data
    }

    function Footer() {
        //Closure line
        //$this->Cell(200,0,'','T');
    }

}


?>
